package core;

import java.io.File;
import java.util.ArrayList;


public class Folds {

	public int kfolds;
	File[] listOfFiles;
	public ArrayList<File>[] foldArray;

	
	public Folds(int numFolds, String dirDados){
		kfolds=numFolds;
		File folder = new File(dirDados);
		listOfFiles = folder.listFiles();
	}
	
	public Folds(int numFolds, File[] listaFicheiros){
		kfolds=numFolds;
		listOfFiles = listaFicheiros;
	}
	
	
	public void init(){
		foldArray = new ArrayList[kfolds];
		for(int i=0; i<kfolds; i++){
			foldArray[i] = new ArrayList<File>();
		}
		
		int j=0;
		for(int i=0; i<listOfFiles.length; i++){
			foldArray[j++].add(listOfFiles[i]);
			if(j==kfolds){
				j=0;
			}
		}
	}
	
	public File[] geraTreinoParaFold(int fold){
		
		ArrayList<File> result = new ArrayList<File>();
		for(int i=0; i<kfolds; i++){
			if(i==fold)
				continue;
			for(File it : foldArray[i]){
				result.add(it);
			}	
		}
		return arrayListToArray(result);
	}
	
	public File[] geraTesteParaFold(int fold){
		File[] result = new File[foldArray[fold].size()];
		int i=0;
		for(File it : foldArray[fold]){
			result[i++] = it;
		}
		return result;
	}
	
	public File[] arrayListToArray(ArrayList<File> arg){
		File[] result = new File[arg.size()];
		int i=0;
		for(File it : arg){
			result[i++] = it;
		}
		return result;
	}
	
		
}
